package trip_rider_log

import (
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/app"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/database"
	"gitee.com/sweetsunnyflower/carpooling-server-go/pkg/paginator"

	"github.com/gofiber/fiber/v2"
)

func Get(id uint64) (tripRiderLog TripRiderLog) {
	database.DB.Where("id", id).First(&tripRiderLog)
	return
}

func GetBy(field, value string) (tripRiderLog TripRiderLog) {
	database.DB.Where("? = ?", field, value).First(&tripRiderLog)
	return
}

func All() (tripRiderLogs []TripRiderLog) {
	database.DB.Order("id desc").Find(&tripRiderLogs)
	return
}

func IsExist(field, value string) bool {
	var count int64
	database.DB.Model(TripRiderLog{}).Where("? = ?", field, value).Count(&count)
	return count > 0
}

// Paginate 分页内容
func Paginate(c *fiber.Ctx, perPage int) (tripRiderLogs []TripRiderLog, paging paginator.Paging) {
	paging = paginator.Paginate(
		c,
		database.DB.Model(TripRiderLog{}),
		&tripRiderLogs,
		app.V1URL(database.TableName(&TripRiderLog{})),
		perPage,
	)
	return
}
